{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "import io\n",
    "import torch\n",
    "import torchvision\n",
    "import torchvision.transforms as transforms\n",
    "import torch.nn as nn\n",
    "import torch.nn.functional as F\n",
    "from torch.autograd import Variable\n",
    "import torch.optim as optim\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "from PIL import Image\n",
    "from torch.utils.tensorboard import SummaryWriter\n",
    "\n",
    "\n",
    "def loadtraindata():\n",
    "    path = r\"./train\"\n",
    "    trainset = torchvision.datasets.ImageFolder(path,\n",
    "                                                transform=transforms.Compose([\n",
    "                                                    transforms.Resize((300, 300)),  # 将图片缩放到指定大小（h,w）\n",
    "                                                    transforms.CenterCrop(300),\n",
    "                                                    transforms.ToTensor()])\n",
    "                                                )\n",
    "    trainloader = torch.utils.data.DataLoader(trainset, batch_size=4,\n",
    "                                              shuffle=True, num_workers=2)\n",
    "    return trainloader\n",
    "\n",
    "class Net(nn.Module):\n",
    "\n",
    "    def __init__(self):\n",
    "        super(Net, self).__init__()\n",
    "        self.conv1 = nn.Conv2d(3, 6, 9)  # 卷积层\n",
    "        self.pool1 = nn.MaxPool2d(2, 2)  # 池化层\n",
    "        self.conv2 = nn.Conv2d(6, 10, 11)  # 卷积层\n",
    "        self.conv3 = nn.Conv2d(10, 6, 9)  # 卷积层\n",
    "        self.conv4 = nn.Conv2d(6, 16, 11)  # 卷积层\n",
    "        self.fc1 = nn.Linear(1600, 480)  # 全连接层\n",
    "        self.fc2 = nn.Linear(480, 120)\n",
    "        self.fc3 = nn.Linear(120, 3)\n",
    "\n",
    "    def forward(self, x):\n",
    "        x = self.pool1(F.relu(self.conv1(x)))\n",
    "        x = self.pool1(F.relu(self.conv2(x)))\n",
    "        x = self.pool1(F.relu(self.conv3(x)))\n",
    "        x = self.pool1(F.relu(self.conv4(x)))\n",
    "        x = x.view(-1, 1600)\n",
    "\n",
    "        x = F.relu(self.fc1(x))\n",
    "        x = F.relu(self.fc2(x))\n",
    "        x = self.fc3(x)\n",
    "        return x\n",
    "\n",
    "\n",
    "classes = ('未知','女', '男')\n",
    "\n",
    "\n",
    "\n",
    "def trainandsave():  # 训练\n",
    "    writer = SummaryWriter('runs/ManOrWuman')\n",
    "    trainloader = loadtraindata()\n",
    "    #net = Net()\n",
    "    net = reload_net()\n",
    "    optimizer = optim.SGD(net.parameters(), lr=0.01, momentum=0.001)\n",
    "    criterion = nn.CrossEntropyLoss()\n",
    "    for epoch in range(500):\n",
    "        running_loss = 0.0\n",
    "        for i, data in enumerate(trainloader, 0):\n",
    "            inputs, labels = data\n",
    "            inputs, labels = Variable(inputs), Variable(labels)\n",
    "\n",
    "            img_grid = torchvision.utils.make_grid(inputs)\n",
    "            writer.add_image('Man', img_grid)\n",
    "            optimizer.zero_grad()\n",
    "            outputs = net(inputs)\n",
    "            loss = criterion(outputs, labels)\n",
    "            loss.backward()\n",
    "            optimizer.step()\n",
    "            running_loss += loss.item()\n",
    "            if i % 20 == 19:\n",
    "                writer.add_scalar('training loss',running_loss / 20,epoch * len(trainloader) + i)\n",
    "                # writer.add_figure('predictions vs. actuals',plot_classes_preds(net, inputs, labels),global_step=epoch * len(trainloader) + i)\n",
    "                print('[%d, %5d] loss: %.3f' % (epoch + 1, i + 1, running_loss / 20))\n",
    "                running_loss = 0.0\n",
    "        print(str(epoch) + '  OK!')\n",
    "        torch.save(net, './out/net'+str(epoch)+'.pkl')\n",
    "        torch.save(net.state_dict(), './out/net_params'+str(epoch)+'.pkl')\n",
    "\n",
    "    writer.add_graph(Net(), inputs)\n",
    "    writer.close()\n",
    "    print('Finished Training')\n",
    "\n",
    "\n",
    "\n",
    "def reload_net():\n",
    "    trainednet = torch.load('./out/net23.pkl')\n",
    "    return trainednet\n",
    "\n",
    "\n",
    "def test_one(img_path):\n",
    "    model = reload_net()\n",
    "    transform_valid = transforms.Compose([\n",
    "        transforms.Resize((300, 300), interpolation=4),\n",
    "        transforms.ToTensor()\n",
    "    ]\n",
    "    )\n",
    "    img = Image.open(img_path)\n",
    "    img_ = transform_valid(img).unsqueeze(0)\n",
    "    outputs = model(img_)\n",
    "    _, indices = torch.max(outputs, 1)\n",
    "    #print(indices)\n",
    "    result = classes[indices]\n",
    "    #print('我认为这个东西的性别是:', result)\n",
    "    #print(outputs)\n",
    "    return result\n",
    "\n",
    "def main():\n",
    "    trainandsave()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([1])\n",
      "我认为这个东西的性别是: 女\n",
      "tensor([[-5.7683,  5.9619, -0.3153]], grad_fn=<AddmmBackward>)\n"
     ]
    }
   ],
   "source": [
    "test_one(r'./train/女/tet16.jpg')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1,    20] loss: 0.266\n",
      "[1,    40] loss: 0.191\n",
      "0  OK!\n",
      "[2,    20] loss: 0.058\n",
      "[2,    40] loss: 0.041\n",
      "1  OK!\n",
      "[3,    20] loss: 0.007\n",
      "[3,    40] loss: 0.026\n",
      "2  OK!\n",
      "[4,    20] loss: 0.004\n",
      "[4,    40] loss: 0.022\n",
      "3  OK!\n",
      "[5,    20] loss: 0.001\n",
      "[5,    40] loss: 0.026\n",
      "4  OK!\n",
      "[6,    20] loss: 0.006\n",
      "[6,    40] loss: 0.013\n",
      "5  OK!\n",
      "[7,    20] loss: 0.000\n",
      "[7,    40] loss: 0.015\n",
      "6  OK!\n",
      "[8,    20] loss: 0.014\n",
      "[8,    40] loss: 0.009\n",
      "7  OK!\n",
      "[9,    20] loss: 0.001\n",
      "[9,    40] loss: 0.012\n",
      "8  OK!\n",
      "[10,    20] loss: 0.020\n",
      "[10,    40] loss: 0.005\n",
      "9  OK!\n",
      "[11,    20] loss: 0.000\n",
      "[11,    40] loss: 0.012\n",
      "10  OK!\n",
      "[12,    20] loss: 0.000\n",
      "[12,    40] loss: 0.008\n",
      "11  OK!\n",
      "[13,    20] loss: 0.000\n",
      "[13,    40] loss: 0.006\n",
      "12  OK!\n",
      "[14,    20] loss: 0.009\n",
      "[14,    40] loss: 0.001\n",
      "13  OK!\n",
      "[15,    20] loss: 0.000\n",
      "[15,    40] loss: 0.004\n",
      "14  OK!\n",
      "[16,    20] loss: 0.001\n",
      "[16,    40] loss: 0.000\n",
      "15  OK!\n",
      "[17,    20] loss: 0.000\n",
      "[17,    40] loss: 0.001\n",
      "16  OK!\n",
      "[18,    20] loss: 0.000\n",
      "[18,    40] loss: 0.001\n",
      "17  OK!\n",
      "[19,    20] loss: 0.000\n",
      "[19,    40] loss: 0.000\n",
      "18  OK!\n",
      "[20,    20] loss: 0.000\n",
      "[20,    40] loss: 0.000\n",
      "19  OK!\n",
      "[21,    20] loss: 0.000\n",
      "[21,    40] loss: 0.000\n",
      "20  OK!\n",
      "[22,    20] loss: 0.000\n",
      "[22,    40] loss: 0.000\n",
      "21  OK!\n",
      "[23,    20] loss: 0.000\n",
      "[23,    40] loss: 0.000\n",
      "22  OK!\n",
      "[24,    20] loss: 0.000\n",
      "[24,    40] loss: 0.000\n",
      "23  OK!\n",
      "[25,    20] loss: 0.000\n"
     ]
    },
    {
     "ename": "KeyboardInterrupt",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyboardInterrupt\u001b[0m                         Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-7-263240bbee7e>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mmain\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;32m<ipython-input-5-ea7c2ffce765>\u001b[0m in \u001b[0;36mmain\u001b[1;34m()\u001b[0m\n\u001b[0;32m    114\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    115\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0mmain\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 116\u001b[1;33m     \u001b[0mtrainandsave\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;32m<ipython-input-5-ea7c2ffce765>\u001b[0m in \u001b[0;36mtrainandsave\u001b[1;34m()\u001b[0m\n\u001b[0;32m     73\u001b[0m             \u001b[0moutputs\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mnet\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0minputs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     74\u001b[0m             \u001b[0mloss\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mcriterion\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0moutputs\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mlabels\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 75\u001b[1;33m             \u001b[0mloss\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mbackward\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     76\u001b[0m             \u001b[0moptimizer\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mstep\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     77\u001b[0m             \u001b[0mrunning_loss\u001b[0m \u001b[1;33m+=\u001b[0m \u001b[0mloss\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mitem\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mD:\\Anaconda3\\lib\\site-packages\\torch\\tensor.py\u001b[0m in \u001b[0;36mbackward\u001b[1;34m(self, gradient, retain_graph, create_graph, inputs)\u001b[0m\n\u001b[0;32m    243\u001b[0m                 \u001b[0mcreate_graph\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mcreate_graph\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    244\u001b[0m                 inputs=inputs)\n\u001b[1;32m--> 245\u001b[1;33m         \u001b[0mtorch\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mautograd\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mbackward\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mgradient\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mretain_graph\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mcreate_graph\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0minputs\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0minputs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    246\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    247\u001b[0m     \u001b[1;32mdef\u001b[0m \u001b[0mregister_hook\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mhook\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mD:\\Anaconda3\\lib\\site-packages\\torch\\autograd\\__init__.py\u001b[0m in \u001b[0;36mbackward\u001b[1;34m(tensors, grad_tensors, retain_graph, create_graph, grad_variables, inputs)\u001b[0m\n\u001b[0;32m    143\u001b[0m         \u001b[0mretain_graph\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mcreate_graph\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    144\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 145\u001b[1;33m     Variable._execution_engine.run_backward(\n\u001b[0m\u001b[0;32m    146\u001b[0m         \u001b[0mtensors\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mgrad_tensors_\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mretain_graph\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mcreate_graph\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0minputs\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    147\u001b[0m         allow_unreachable=True, accumulate_grad=True)  # allow_unreachable flag\n",
      "\u001b[1;31mKeyboardInterrupt\u001b[0m: "
     ]
    }
   ],
   "source": [
    "main()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([1])\n",
      "我认为这个东西的性别是: 女\n",
      "tensor([[-5.0814,  5.2480, -0.5318]], grad_fn=<AddmmBackward>)\n",
      "tensor([1])\n",
      "我认为这个东西的性别是: 女\n",
      "tensor([[-5.5593,  7.1158, -1.5500]], grad_fn=<AddmmBackward>)\n",
      "tensor([1])\n",
      "我认为这个东西的性别是: 女\n",
      "tensor([[-7.4239,  6.5711,  1.1758]], grad_fn=<AddmmBackward>)\n",
      "tensor([1])\n",
      "我认为这个东西的性别是: 女\n",
      "tensor([[-1.6561,  1.3235,  0.3479]], grad_fn=<AddmmBackward>)\n",
      "tensor([1])\n",
      "我认为这个东西的性别是: 女\n",
      "tensor([[-2.3210,  1.7635,  0.5538]], grad_fn=<AddmmBackward>)\n",
      "tensor([1])\n",
      "我认为这个东西的性别是: 女\n",
      "tensor([[-2.7614,  1.6235,  0.8059]], grad_fn=<AddmmBackward>)\n",
      "tensor([1])\n",
      "我认为这个东西的性别是: 女\n",
      "tensor([[-4.8470,  3.5484,  1.6563]], grad_fn=<AddmmBackward>)\n",
      "tensor([1])\n",
      "我认为这个东西的性别是: 女\n",
      "tensor([[-7.1622,  7.7006, -0.0117]], grad_fn=<AddmmBackward>)\n",
      "tensor([1])\n",
      "我认为这个东西的性别是: 女\n",
      "tensor([[-4.3697,  4.8423, -0.5957]], grad_fn=<AddmmBackward>)\n",
      "tensor([1])\n",
      "我认为这个东西的性别是: 女\n",
      "tensor([[-4.4308,  4.7253, -0.7929]], grad_fn=<AddmmBackward>)\n",
      "tensor([1])\n",
      "我认为这个东西的性别是: 女\n",
      "tensor([[-5.4868,  7.0378, -1.6435]], grad_fn=<AddmmBackward>)\n",
      "tensor([1])\n",
      "我认为这个东西的性别是: 女\n",
      "tensor([[-2.7965,  2.1085,  0.3448]], grad_fn=<AddmmBackward>)\n",
      "tensor([1])\n",
      "我认为这个东西的性别是: 女\n",
      "tensor([[-5.6710,  5.9762, -0.2414]], grad_fn=<AddmmBackward>)\n",
      "tensor([1])\n",
      "我认为这个东西的性别是: 女\n",
      "tensor([[-3.7699,  3.7922, -0.2894]], grad_fn=<AddmmBackward>)\n",
      "tensor([1])\n",
      "我认为这个东西的性别是: 女\n",
      "tensor([[-7.5233,  8.2352, -1.5442]], grad_fn=<AddmmBackward>)\n",
      "tensor([1])\n",
      "我认为这个东西的性别是: 女\n",
      "tensor([[-4.0698,  3.7433, -0.1415]], grad_fn=<AddmmBackward>)\n",
      "tensor([1])\n",
      "我认为这个东西的性别是: 女\n",
      "tensor([[-2.6345,  2.2580,  0.5347]], grad_fn=<AddmmBackward>)\n",
      "tensor([1])\n",
      "我认为这个东西的性别是: 女\n",
      "tensor([[-5.9310,  6.9921, -1.1437]], grad_fn=<AddmmBackward>)\n",
      "tensor([1])\n",
      "我认为这个东西的性别是: 女\n",
      "tensor([[-3.2061,  3.1528, -0.0220]], grad_fn=<AddmmBackward>)\n",
      "tensor([1])\n",
      "我认为这个东西的性别是: 女\n",
      "tensor([[-4.2886,  5.0156, -1.3140]], grad_fn=<AddmmBackward>)\n",
      "tensor([1])\n",
      "我认为这个东西的性别是: 女\n",
      "tensor([[-5.2065,  5.8223,  0.4290]], grad_fn=<AddmmBackward>)\n",
      "tensor([1])\n",
      "我认为这个东西的性别是: 女\n",
      "tensor([[-5.3318,  6.0748, -0.6103]], grad_fn=<AddmmBackward>)\n",
      "tensor([1])\n",
      "我认为这个东西的性别是: 女\n",
      "tensor([[-11.8893,  12.4276,  -2.6454]], grad_fn=<AddmmBackward>)\n",
      "tensor([1])\n",
      "我认为这个东西的性别是: 女\n",
      "tensor([[-9.1227, 10.2949, -2.0739]], grad_fn=<AddmmBackward>)\n",
      "tensor([1])\n",
      "我认为这个东西的性别是: 女\n",
      "tensor([[-4.5940,  4.4742, -0.3751]], grad_fn=<AddmmBackward>)\n",
      "tensor([1])\n",
      "我认为这个东西的性别是: 女\n",
      "tensor([[-8.9656, 13.0360, -3.4289]], grad_fn=<AddmmBackward>)\n",
      "tensor([1])\n",
      "我认为这个东西的性别是: 女\n",
      "tensor([[-5.6254,  6.3818, -0.9505]], grad_fn=<AddmmBackward>)\n",
      "tensor([1])\n",
      "我认为这个东西的性别是: 女\n",
      "tensor([[-10.3097,  15.0962,  -3.0021]], grad_fn=<AddmmBackward>)\n",
      "tensor([1])\n",
      "我认为这个东西的性别是: 女\n",
      "tensor([[-13.0385,  15.5677,  -3.1778]], grad_fn=<AddmmBackward>)\n",
      "tensor([1])\n",
      "我认为这个东西的性别是: 女\n",
      "tensor([[-13.3302,  14.2326,  -2.8813]], grad_fn=<AddmmBackward>)\n",
      "tensor([1])\n",
      "我认为这个东西的性别是: 女\n",
      "tensor([[-3.7656,  4.3626, -0.2557]], grad_fn=<AddmmBackward>)\n",
      "tensor([1])\n",
      "我认为这个东西的性别是: 女\n",
      "tensor([[-2.2781,  1.7116,  0.2291]], grad_fn=<AddmmBackward>)\n",
      "tensor([1])\n",
      "我认为这个东西的性别是: 女\n",
      "tensor([[-5.0873,  2.5438,  1.5568]], grad_fn=<AddmmBackward>)\n",
      "tensor([1])\n",
      "我认为这个东西的性别是: 女\n",
      "tensor([[-3.9705,  3.2846,  0.0791]], grad_fn=<AddmmBackward>)\n",
      "tensor([1])\n",
      "我认为这个东西的性别是: 女\n",
      "tensor([[-7.9811,  6.8435,  1.2972]], grad_fn=<AddmmBackward>)\n",
      "tensor([1])\n",
      "我认为这个东西的性别是: 女\n",
      "tensor([[-8.2761,  9.3605, -1.2981]], grad_fn=<AddmmBackward>)\n",
      "tensor([1])\n",
      "我认为这个东西的性别是: 女\n",
      "tensor([[-3.6393,  4.9894, -0.6064]], grad_fn=<AddmmBackward>)\n",
      "tensor([1])\n",
      "我认为这个东西的性别是: 女\n",
      "tensor([[-4.7597,  3.3804,  0.4232]], grad_fn=<AddmmBackward>)\n",
      "tensor([1])\n",
      "我认为这个东西的性别是: 女\n",
      "tensor([[-1.3364,  0.7375,  0.5096]], grad_fn=<AddmmBackward>)\n",
      "tensor([2])\n",
      "我认为这个东西的性别是: 男\n",
      "tensor([[-1.9824,  0.8718,  0.9104]], grad_fn=<AddmmBackward>)\n",
      "tensor([1])\n",
      "我认为这个东西的性别是: 女\n",
      "tensor([[-14.2597,  16.6610,  -2.5346]], grad_fn=<AddmmBackward>)\n",
      "tensor([1])\n",
      "我认为这个东西的性别是: 女\n",
      "tensor([[-4.6667,  3.3660,  0.0667]], grad_fn=<AddmmBackward>)\n",
      "tensor([1])\n",
      "我认为这个东西的性别是: 女\n",
      "tensor([[-7.4490,  8.4890, -1.4114]], grad_fn=<AddmmBackward>)\n",
      "tensor([1])\n",
      "我认为这个东西的性别是: 女\n",
      "tensor([[-3.8334,  3.6888,  0.6992]], grad_fn=<AddmmBackward>)\n",
      "tensor([1])\n",
      "我认为这个东西的性别是: 女\n",
      "tensor([[-8.7315,  9.7982, -0.9598]], grad_fn=<AddmmBackward>)\n",
      "tensor([1])\n",
      "我认为这个东西的性别是: 女\n",
      "tensor([[-2.9938,  1.5552,  1.0581]], grad_fn=<AddmmBackward>)\n",
      "tensor([1])\n",
      "我认为这个东西的性别是: 女\n",
      "tensor([[-4.3224,  4.0812, -0.0380]], grad_fn=<AddmmBackward>)\n",
      "tensor([1])\n",
      "我认为这个东西的性别是: 女\n",
      "tensor([[-3.1441,  2.6461,  0.4354]], grad_fn=<AddmmBackward>)\n",
      "tensor([1])\n",
      "我认为这个东西的性别是: 女\n",
      "tensor([[-8.4425,  7.9125, -1.0139]], grad_fn=<AddmmBackward>)\n",
      "tensor([1])\n",
      "我认为这个东西的性别是: 女\n",
      "tensor([[-4.5311,  5.2823, -0.4055]], grad_fn=<AddmmBackward>)\n",
      "tensor([1])\n",
      "我认为这个东西的性别是: 女\n",
      "tensor([[-4.3310,  5.7983, -1.1094]], grad_fn=<AddmmBackward>)\n",
      "tensor([1])\n",
      "我认为这个东西的性别是: 女\n",
      "tensor([[-4.9345,  5.1319, -0.0699]], grad_fn=<AddmmBackward>)\n",
      "tensor([1])\n",
      "我认为这个东西的性别是: 女\n",
      "tensor([[-3.7041,  2.8671, -0.2603]], grad_fn=<AddmmBackward>)\n",
      "tensor([1])\n",
      "我认为这个东西的性别是: 女\n",
      "tensor([[-4.7298,  4.5170, -0.0774]], grad_fn=<AddmmBackward>)\n",
      "tensor([1])\n",
      "我认为这个东西的性别是: 女\n",
      "tensor([[-8.6051,  8.0385,  0.9283]], grad_fn=<AddmmBackward>)\n",
      "tensor([1])\n",
      "我认为这个东西的性别是: 女\n",
      "tensor([[-4.8274,  6.8376, -1.6095]], grad_fn=<AddmmBackward>)\n",
      "tensor([1])\n",
      "我认为这个东西的性别是: 女\n",
      "tensor([[-7.0443,  6.2658,  0.1736]], grad_fn=<AddmmBackward>)\n",
      "tensor([1])\n",
      "我认为这个东西的性别是: 女\n",
      "tensor([[-12.7535,  13.1156,  -3.3323]], grad_fn=<AddmmBackward>)\n",
      "tensor([1])\n",
      "我认为这个东西的性别是: 女\n",
      "tensor([[-4.4235,  4.7114, -0.7693]], grad_fn=<AddmmBackward>)\n",
      "tensor([1])\n",
      "我认为这个东西的性别是: 女\n",
      "tensor([[-6.3258,  5.5160, -0.4140]], grad_fn=<AddmmBackward>)\n",
      "tensor([1])\n",
      "我认为这个东西的性别是: 女\n",
      "tensor([[-11.8683,  11.2683,  -1.8981]], grad_fn=<AddmmBackward>)\n",
      "tensor([1])\n",
      "我认为这个东西的性别是: 女\n",
      "tensor([[-7.0411, 10.3041, -3.4418]], grad_fn=<AddmmBackward>)\n",
      "tensor([1])\n",
      "我认为这个东西的性别是: 女\n",
      "tensor([[-5.2382,  4.9237, -0.3666]], grad_fn=<AddmmBackward>)\n",
      "tensor([1])\n",
      "我认为这个东西的性别是: 女\n",
      "tensor([[-4.1603,  4.1902, -0.2652]], grad_fn=<AddmmBackward>)\n",
      "tensor([1])\n",
      "我认为这个东西的性别是: 女\n",
      "tensor([[-1.7174,  1.1439,  0.3335]], grad_fn=<AddmmBackward>)\n",
      "tensor([1])\n",
      "我认为这个东西的性别是: 女\n",
      "tensor([[-3.2318,  3.2063, -0.0416]], grad_fn=<AddmmBackward>)\n",
      "tensor([1])\n",
      "我认为这个东西的性别是: 女\n",
      "tensor([[-1.3512,  0.7320,  0.5406]], grad_fn=<AddmmBackward>)\n",
      "tensor([1])\n",
      "我认为这个东西的性别是: 女\n",
      "tensor([[-8.0473, 10.9675, -1.7458]], grad_fn=<AddmmBackward>)\n",
      "tensor([2])\n",
      "我认为这个东西的性别是: 男\n",
      "tensor([[-3.5724,  1.1731,  2.0687]], grad_fn=<AddmmBackward>)\n",
      "tensor([1])\n",
      "我认为这个东西的性别是: 女\n",
      "tensor([[-5.7683,  5.9619, -0.3153]], grad_fn=<AddmmBackward>)\n",
      "tensor([1])\n",
      "我认为这个东西的性别是: 女\n",
      "tensor([[-5.6267,  6.3858, -0.3906]], grad_fn=<AddmmBackward>)\n",
      "tensor([1])\n",
      "我认为这个东西的性别是: 女\n",
      "tensor([[-14.7210,  22.4803,  -6.8434]], grad_fn=<AddmmBackward>)\n",
      "tensor([1])\n",
      "我认为这个东西的性别是: 女\n",
      "tensor([[-3.6382,  3.5781,  0.7173]], grad_fn=<AddmmBackward>)\n",
      "tensor([1])\n",
      "我认为这个东西的性别是: 女\n",
      "tensor([[-6.5502,  9.1032, -2.3029]], grad_fn=<AddmmBackward>)\n",
      "tensor([1])\n",
      "我认为这个东西的性别是: 女\n",
      "tensor([[-11.4657,  13.7834,  -3.8391]], grad_fn=<AddmmBackward>)\n",
      "tensor([1])\n",
      "我认为这个东西的性别是: 女\n",
      "tensor([[-10.9713,   9.3595,  -1.5014]], grad_fn=<AddmmBackward>)\n",
      "tensor([1])\n",
      "我认为这个东西的性别是: 女\n",
      "tensor([[-2.4890,  2.0551,  0.3005]], grad_fn=<AddmmBackward>)\n",
      "tensor([2])\n",
      "我认为这个东西的性别是: 男\n",
      "tensor([[-5.7673,  2.2200,  2.8686]], grad_fn=<AddmmBackward>)\n",
      "tensor([1])\n",
      "我认为这个东西的性别是: 女\n",
      "tensor([[-2.8170,  1.4246,  1.1729]], grad_fn=<AddmmBackward>)\n",
      "tensor([1])\n",
      "我认为这个东西的性别是: 女\n",
      "tensor([[-6.9008,  6.7107, -0.8122]], grad_fn=<AddmmBackward>)\n",
      "tensor([1])\n",
      "我认为这个东西的性别是: 女\n",
      "tensor([[-3.9814,  4.3143, -0.5145]], grad_fn=<AddmmBackward>)\n",
      "tensor([1])\n",
      "我认为这个东西的性别是: 女\n",
      "tensor([[-5.5426,  5.7901,  0.4957]], grad_fn=<AddmmBackward>)\n",
      "tensor([1])\n",
      "我认为这个东西的性别是: 女\n",
      "tensor([[-4.9001,  5.1258, -0.0939]], grad_fn=<AddmmBackward>)\n",
      "tensor([1])\n",
      "我认为这个东西的性别是: 女\n",
      "tensor([[-5.8583,  8.9593, -1.3016]], grad_fn=<AddmmBackward>)\n",
      "tensor([1])\n",
      "我认为这个东西的性别是: 女\n",
      "tensor([[-7.4248,  9.1345, -1.4580]], grad_fn=<AddmmBackward>)\n",
      "tensor([1])\n",
      "我认为这个东西的性别是: 女\n",
      "tensor([[-2.3835,  1.5916,  0.4624]], grad_fn=<AddmmBackward>)\n",
      "tensor([1])\n",
      "我认为这个东西的性别是: 女\n",
      "tensor([[-8.4316,  9.8252, -1.4243]], grad_fn=<AddmmBackward>)\n",
      "tensor([1])\n",
      "我认为这个东西的性别是: 女\n",
      "tensor([[-3.9052,  5.4015, -1.0324]], grad_fn=<AddmmBackward>)\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "os.listdir(\"train\\女\")\n",
    "for name in os.listdir(\"train\\女\"):\n",
    "    datali = \"train\\女/\"\n",
    "    test_one(datali + name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "女\n",
      "女\n",
      "女\n",
      "女\n",
      "女\n",
      "女\n",
      "女\n",
      "女\n",
      "女\n",
      "女\n",
      "女\n",
      "女\n",
      "女\n",
      "女\n",
      "女\n",
      "女\n",
      "女\n",
      "女\n",
      "女\n",
      "女\n",
      "女\n",
      "女\n",
      "女\n",
      "女\n",
      "女\n",
      "女\n",
      "女\n",
      "女\n",
      "女\n",
      "女\n",
      "女\n",
      "女\n",
      "女\n",
      "女\n",
      "女\n",
      "女\n",
      "女\n",
      "女\n",
      "女\n",
      "女\n",
      "女\n",
      "女\n",
      "女\n",
      "女\n",
      "女\n",
      "女\n",
      "女\n",
      "女\n",
      "女\n",
      "女\n",
      "女\n",
      "女\n",
      "女\n",
      "女\n",
      "女\n",
      "女\n",
      "女\n",
      "女\n",
      "女\n",
      "女\n",
      "女\n",
      "女\n",
      "女\n",
      "女\n",
      "女\n",
      "女\n",
      "女\n",
      "女\n",
      "女\n",
      "女\n",
      "女\n",
      "女\n",
      "女\n",
      "女\n",
      "女\n",
      "女\n",
      "女\n",
      "女\n",
      "女\n",
      "女\n",
      "女\n",
      "女\n",
      "女\n",
      "女\n",
      "女\n",
      "女\n",
      "女\n",
      "女\n",
      "女\n",
      "女\n",
      "女\n",
      "女\n",
      "女\n",
      "女\n",
      "女\n",
      "女\n",
      "女\n",
      "女\n",
      "女\n",
      "女\n",
      "女\n",
      "女\n",
      "女\n",
      "女\n",
      "女\n",
      "女\n",
      "女\n",
      "女\n",
      "女\n",
      "女\n"
     ]
    }
   ],
   "source": [
    "import cv2 as cv\n",
    "import os\n",
    "\n",
    "cap = cv.VideoCapture(0)\n",
    "while True:\n",
    "    ret,frame = cap.read()\n",
    "    cv.imwrite('RAM.jpg',frame)\n",
    "    if test_one('RAM.jpg') == '女':\n",
    "        print('女')\n",
    "    cv.imshow('win',frame)\n",
    "    os.remove('RAM.jpg')\n",
    "    os.system('cls')\n",
    "    if cv.waitKey(1) & 0xFF == ord('q'):\n",
    "        break\n",
    "cap.release()\n",
    "cv.destroyAllWindows()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
